﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using HotelManageSystem.Domain;
using HotelManageSystem.Domain.Entities;
using HotelManageSystem.Domain.Infrastructure;
using HotelManageSystem.Models;
using Telerik.Web.Mvc;

namespace HotelManageSystem.Controllers
{
    public class CheckOutController : Controller
    {
        //
        // GET: /CheckOut/
        private readonly IProductRepository productRepository;
        private readonly IRoomRepository roomRepository;
        private readonly IClientConsumptionRepository clientConsumptionRepository;
        private readonly IClientRepository clientRepository;
        private readonly IClientTypeRepository clientTypeRepository;
        private readonly IHotelingInfomationRepository hotelingInfomationRepository;
        private readonly IHotelingHistoryRepository hotelingHistoryRepository;
        private readonly IRoomStateRepository roomStateRepository;
        private readonly IRoomTypeRepository roomTypeRepository;
        private readonly IUnitOfWork unitOfWork;

        public CheckOutController(IProductRepository productRepository, IRoomRepository roomRepository, IClientConsumptionRepository clientConsumptionRepository, IClientRepository clientRepository, IClientTypeRepository clientTypeRepository, IHotelingInfomationRepository hotelingInfomationRepository, IHotelingHistoryRepository hotelingHistoryRepository, IRoomStateRepository roomStateRepository, IRoomTypeRepository roomTypeRepository, IUnitOfWork unitOfWork)
        {
            this.productRepository = productRepository;
            this.roomRepository = roomRepository;
            this.clientConsumptionRepository = clientConsumptionRepository;
            this.clientRepository = clientRepository;
            this.clientTypeRepository = clientTypeRepository;
            this.hotelingInfomationRepository = hotelingInfomationRepository;
            this.hotelingHistoryRepository = hotelingHistoryRepository;
            this.roomStateRepository = roomStateRepository;
            this.roomTypeRepository = roomTypeRepository;
            this.unitOfWork = unitOfWork;
        }
        private IList<CheckOutInfoModel> GetCheckOutInfo()
        {
            Session["roomId"] = Request["roomId"];
            string roomId = Session["roomId"].ToString();
            var clientConsumptionInfo = clientConsumptionRepository.GetMany(x => x.Room.ID == roomId);
            var clientInfo = clientRepository.GetMany(x => x.Room.ID == roomId && x.IsCheckOut==false);
            List<string> clientId = new List<string>();
            List<string> clientName = new List<string>();
            foreach (var client in clientInfo)
            {
                clientId.Add(client.ID);
                clientName.Add(client.Name);
            }
            if (clientInfo != null && clientInfo.Count() != 0)
            {
                if (clientInfo.Count() > 1)
                {
                    ViewBag.clientId = clientId[0];
                    ViewBag.clientName = clientName[0];

                    ViewBag.clientId2 = clientId[1];
                    ViewBag.clientName2 = clientName[1];
                }
                else
                {
                    ViewBag.clientId = clientId[0];
                    ViewBag.clientName = clientName[0];
                }
            }
            decimal sumAccount = 0;
            foreach (var clientConsumption in clientConsumptionInfo)
            {
                sumAccount += clientConsumption.ProductCost;
            }
            ViewBag.SumAccount = sumAccount;
            ViewBag.DateTime = DateTime.Now;
            decimal advancePayment=hotelingInfomationRepository.Get(x => x.Room.ID == roomId).AdvancePayment;
            ViewBag.AdvancePayment = advancePayment.ToString();
            ViewBag.roomId = roomId;
            //房间价格
            decimal roomPrice = roomRepository.Get(x => x.ID == roomId).RoomType.Price;
            //打折价格
            decimal discount = Convert.ToDecimal(clientRepository.Get(x => x.Room.ID == roomId).ClientType.Discount);
            decimal roomPayment = roomPrice * discount;
            ViewBag.discountPrice = roomPayment;
            ViewBag.roomPrice = roomPrice;
            //还需要支付的金额
            ViewBag.NeedRepay = roomPayment + sumAccount - advancePayment;
            List<CheckOutInfoModel> checkOutInfo = (from ccInfo in clientConsumptionInfo
                                                    select new CheckOutInfoModel
                                                               {
                                                                   ProductId = ccInfo.Product.ID,
                                                                   ProductName = ccInfo.Product.Name,
                                                                   RoomId = Session["roomId"].ToString(),
                                                                   PriceOut = ccInfo.Product.PriceOut,
                                                                   ProductCount = ccInfo.ProductCount,
                                                                   ProductCost = ccInfo.ProductCost,
                                                                   SumAccount = sumAccount,
                                                                   ConsumeDate = ccInfo.ConsumeDate,
                                                                   CheckOutTime = DateTime.Now

                                                               }).ToList();
            return checkOutInfo;
        }
        private void AddHotelingHistoryInfo()
        {
            string roomId = Session["roomId"].ToString();
            var hotelHistory = new HotelingHistory();
            
            hotelHistory.ID = "RZLS"+DateTime.Now.ToString();

            hotelHistory.Room = roomRepository.Get(x => x.ID == roomId);
            var clientInfo = clientRepository.GetMany(x => x.Room.ID == roomId);
            hotelHistory.ClientCollection = clientInfo.ToArray();

            var hotelingInfo = hotelingInfomationRepository.Get(x => x.Room.ID == roomId);
            hotelHistory.PresonNum = hotelingInfo.PresonNum;
            hotelHistory.EntetTime = hotelingInfo.EntetTime;
            hotelHistory.OutTime = DateTime.Now;
            

            hotelingHistoryRepository.Add(hotelHistory);
            unitOfWork.Commit();
        }
        private void DeleteInfo()
        {
            string roomId = Session["roomId"].ToString();
            var clientInfo = clientRepository.GetMany(x => x.Room.ID == roomId);
            var clientConsumptionInfo = clientConsumptionRepository.GetMany(x => x.Room.ID == roomId);
            var hotelingInfo = hotelingInfomationRepository.Get(x => x.Room.ID == roomId);
            //将退房的用户信息中IsCheckOut字段设置为true
            foreach (var client in clientInfo)
            {
                client.IsCheckOut = true;
                clientRepository.Update(client);
                unitOfWork.Commit();
            }

            foreach (var clientConsumption in clientConsumptionInfo)
            {
                clientConsumptionRepository.Delete(clientConsumption);
                unitOfWork.Commit();
            }

            hotelingInfomationRepository.Delete(hotelingInfo);
            unitOfWork.Commit();

        }
        private void ChangeRoomSate()
        {
            var room= roomRepository.Get(x => x.ID == Session["roomId"].ToString());
            room.RoomState = roomStateRepository.Get(x => x.ID == 4);
            roomRepository.Update(room);
            unitOfWork.Commit();
        }

        public ActionResult CheckOut()
        {
            return View(GetCheckOutInfo());
        }
        [GridAction]
        public ActionResult TelerikAjax()
        {
            return View(new GridModel(GetCheckOutInfo()));
        }
        [HttpPost]
        public void CheckOutAtLast()
        {
            //插入入住历史
            AddHotelingHistoryInfo();
            //删除信息
            DeleteInfo();
            //更改房间状态
            ChangeRoomSate();
        }
    }
}
